﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data.SQLite;

using phpBB;
using System.Net;
using System.Collections;
using System.Data;
using Microsoft.Win32;
using System.IO;

namespace Forums_spamer
{
    /// <summary>
    /// Interaction logic for Shell.xaml
    /// </summary>
    public partial class Shell : Window
    {
        private string webpageSource = string.Empty;
        private string pageCharset = String.Empty;

        private SiteObject _site = new SiteObject();
        private Forum _forum = new Forum();
        private User _user = new User();
        private List<SiteObject> dataCollection;
        private System.Collections.Specialized.NameValueCollection dataToSend = new System.Collections.Specialized.NameValueCollection();
        private byte[] responseBytes;

        private static int currentID = -1;

        public Shell()
        {
            InitializeComponent();
            loginTextBlock.IsReadOnly = true;
            passwordTextBlock.IsEnabled = true;
            domainTextBox.IsReadOnly = true;
            AllGrid.Visibility = Visibility.Hidden;
            dataCollection = new List<SiteObject>();
        }
        private void FillGrid()
        {
            string dbFileName;
            DataTable DT = new DataTable();
            webpageSource = String.Empty;

            string queryData;
            DataBaseObjects objects;

            OpenFileDialog ofd = new OpenFileDialog();
            ofd.ShowDialog();
            if (File.Exists(ofd.FileName))
            {
                dbFileName = ofd.FileName;
                objects = new DataBaseObjects("Data Source=" + dbFileName + ";Version=3;New=False;Compress=True;");
                queryData = @"SELECT
  SITE.id, 
  SITE.Name,
  SITE.url,
  SITE_TYPE.name,
  USER.login,
  USER.password,
  FORUM.name,
  FORUM.link

FROM
 FORUM
 INNER JOIN SITE ON (FORUM.site_id=SITE.id)
 INNER JOIN SITE_TYPE ON (SITE.sitetype_id=SITE_TYPE.id)
 INNER JOIN USER ON (SITE.user_id=USER.id)";
                DT = objects.GetData(queryData);
                int i = -1;
                int j = -1;
                bool isRepeat = false;
                foreach (DataRow tr in DT.Rows)
                {
                    if (_site.Name == tr.ItemArray[1].ToString())
                    {
                        _forum = new Forum();
                        foreach (object item in tr.ItemArray)
                        {
                            i++;
                            switch (i)
                            {
                                case 6:
                                    _forum.Name = item.ToString();
                                    break;
                                case 7:
                                    _forum.Url = item.ToString();
                                    break;
                            }
                        }
                        _site.Forums.Add(_forum);
                        isRepeat = true;
                    }
                    else
                    {
                        _site = new SiteObject();
                        _site.Forums = new List<Forum>();
                        _forum = new Forum();
                        _user = new User();
                        j++;
                        foreach (object item in tr.ItemArray)
                        {
                            i++;
                            switch (i)
                            {
                                case 0:
                                    _site.ID = Convert.ToInt16(item);
                                    break;
                                case 1:
                                    _site.Name = item.ToString();
                                    break;
                                case 2:
                                    _site.Url = item.ToString();
                                    break;
                                case 3:
                                    _site.SiteType = item.ToString();
                                    break;
                                case 4:
                                    _user.Login = item.ToString();
                                    break;
                                case 5:
                                    _user.Password = item.ToString();
                                    break;
                                case 6:
                                    _forum.Name = item.ToString();
                                    break;
                                case 7:
                                    _forum.Url = item.ToString();
                                    break;
                            }
                        }
                        _site.UserObject = _user;
                        _site.Forums.Add(_forum);
                    }
                    if (!isRepeat)
                        dataCollection.Add(_site);
                    else
                    {
                        dataCollection[j] = _site;
                        isRepeat = false;
                    }
                    i = -1;
                }
                currentID = 0;
                AllGrid.DataContext = DT;
                idText.Text = dataCollection[currentID].ID.ToString();
                urlText.Text = dataCollection[currentID].Url;
                nameText.Text = dataCollection[currentID].Name;
                typeText.Text = dataCollection[currentID].SiteType;
                AllGrid.Visibility = Visibility.Visible;

                loginTextBlock.Text = dataCollection[currentID].UserObject.Login;
                //passwordTextBlock.Text = dataCollection[currentID].UserObject.Password;
                domainTextBox.Text = dataCollection[currentID].Url;
            }
        }
        void auth(Object sender)
        {
            if (currentID > -1)
            {
                #region Send POST/GET HTTP data using WebClient with cookies
                // Easy way and enough in most cases
                using (WebClientEnhanced webClient = new WebClientEnhanced())
                {
                    // Below custom charset 
                    //pageCharset =  "ISO-8859-2";
                    //webClient.Encoding = Encoding.GetEncoding(pageCharset);

                    // Phpbb use UTF-8 so we can use build in property
                    // instead some uncommon charset.
                    webClient.Encoding = Encoding.GetEncoding("Windows-1251");

                    // Prepare data (in this case, login credentials) to send.

                    // Variables names are CaseSensitive so must be exactly the same
                    // as in webpage source
                    switch (dataCollection[currentID].SiteType)
                    {
                        case "vBulletin":
                            dataToSend.Add("vb_login_username", dataCollection[currentID].UserObject.Login);
                            dataToSend.Add("vb_login_password", dataCollection[currentID].UserObject.Password);
                            dataToSend.Add("do", "login");
                            responseBytes = webClient.UploadValues(dataCollection[currentID].Url + "/login.php?do=login", WebRequestMethods.Http.Post, dataToSend);
                            webpageSource = webClient.DownloadString(dataCollection[currentID].Url + "/newthread.php?do=newthread&f=" + dataCollection[currentID].Forums[0].ID);
                            break;
                        case "phpBB 2":
                            break;
                        case "phpBB 3":
                            break;
                    }

                    // Often submit button is also nedeed for proper form post
                    // so just add it. Also you can add all other fields from
                    // webpage form but usually it's not needed.
                   

                    // Let's send data by POST method and get the result response
                    

                    // Response return as byte[] array so let's convert it into ordinary string
                    // Here you can check the webpage source by Visual Studio HTMl Visualizer.
                    if (responseBytes != null)
                    {
                        webpageSource = webClient.Encoding.GetString(responseBytes);
                        SiteWebBrowser.NavigateToString(webpageSource);
                    }
                    // Ok, we are logged in succesfully!
                    // Now because we use WebClient enhanced WITH cookies container
                    // so we are able to pull other secured (by session id saved in cookie) 
                    // webpages.
                    // Let's get some profile webpage, for example, User Control Panel
                    //
                    // Our WebClientEnhanced instance has authorization cookies saved so
                    // we can make just simple call.
                    // Here you can check the webpage source by Visual Studio HTMl Visualizer.
                    
                    //posting.php?mode=post&f=4
                    //http://area51.phpbb.com/phpBB/ucp.php?mode=login
                    
                    
                    //SiteWebBrowser.Navigate(uri);
                }
                #endregion
            }
        }

        private void LoginButton_Click(object sender, RoutedEventArgs e)
        {
            auth(sender);
        }

        #region PostButton
        private void PostButton_Click(object sender, RoutedEventArgs e)
        {
            string _user = loginTextBlock.Text;
            string _password ;//= passwordTextBlock.Text;
            
            StringBuilder post = new StringBuilder();
            string _domain = domainTextBox.Text;
            
            string _subject = SubjectTextBox.Text;
            string _message = GetText();

            byte[] data = Encoding.ASCII.GetBytes(post.ToString());
            

            string webpageSource = String.Empty;
            byte[] responseBytes;
            string url = _domain;
            string pageCharset = String.Empty;
            System.Collections.Specialized.NameValueCollection dataToSend = new System.Collections.Specialized.NameValueCollection();
            using (WebClientEnhanced webClient = new WebClientEnhanced())
            {
                webClient.Encoding = Encoding.GetEncoding("Windows-1251");

                // Prepare data (in this case, login credentials) to send.

                // Variables names are CaseSensitive so must be exactly the same
                // as in webpage source
                dataToSend.Add("username", _user);
                dataToSend.Add("password", null);//_password);

                // Often submit button is also nedeed for proper form post
                // so just add it. Also you can add all other fields from
                // webpage form but usually it's not needed.
                dataToSend.Add("login", "Login");

                // Let's send data by POST method and get the result response
                responseBytes = webClient.UploadValues(url, WebRequestMethods.Http.Post, dataToSend);

                url = (_domain.Replace("ucp.php?mode=login", "") + "newthread.php?do=newthread&f=174");
                // Below custom charset 
                //pageCharset =  "ISO-8859-2";
                //webClient.Encoding = Encoding.GetEncoding(pageCharset);

                // Phpbb use UTF-8 so we can use build in property
                // instead some uncommon charset.
                webClient.Encoding = Encoding.GetEncoding("Windows-1251");
                //http://www.proving-grounds.net/forum/login.php?do=login
                // Prepare data (in this case, login credentials) to send.

                // Variables names are CaseSensitive so must be exactly the same
                // as in webpage source
                dataToSend.Clear();
                dataToSend.Add("subject", _subject);

                // Often submit button is also nedeed for proper form post
                // so just add it. Also you can add all other fields from
                // webpage form but usually it's not needed.
                dataToSend.Add("message", _message);
                dataToSend.Add("post", "Submit");
                // Let's send data by POST method and get the result response
                responseBytes = webClient.UploadValues(url, WebRequestMethods.Http.Post, dataToSend);

                // Response return as byte[] array so let's convert it into ordinary string
                // Here you can check the webpage source by Visual Studio HTMl Visualizer.
                webpageSource = webClient.Encoding.GetString(responseBytes);

                // Ok, we are logged in succesfully!
                // Now because we use WebClient enhanced WITH cookies container
                // so we are able to pull other secured (by session id saved in cookie) 
                // webpages.
                // Let's get some profile webpage, for example, User Control Panel
                //url = _domain;

                // Our WebClientEnhanced instance has authorization cookies saved so
                // we can make just simple call.
                // Here you can check the webpage source by Visual Studio HTMl Visualizer.
                webpageSource = webClient.DownloadString(url);
            }

        }
        #endregion

        private string GetText()
        {

            // use a TextRange to fish out the Text from the Document

           // TextRange textRange = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);

            return string.Empty;//textRange.Text;

        }

        private void NextButtonClick(object sender, RoutedEventArgs e)
        {
            if (currentID < dataCollection.Count) currentID++;
            forumComboBox.Items.Clear();
            if (currentID < dataCollection.Count)
            {
                idText.Text = dataCollection[currentID].ID.ToString();
                urlText.Text = dataCollection[currentID].Url;
                nameText.Text = dataCollection[currentID].Name;
                AllGrid.Visibility = Visibility.Visible;
                
                loginTextBlock.Text = dataCollection[currentID].UserObject.Login;
                //passwordTextBlock.a = dataCollection[currentID].UserObject.Password;
                domainTextBox.Text = dataCollection[currentID].Url;
                typeText.Text = dataCollection[currentID].SiteType;
                foreach (Forum fr in dataCollection[currentID].Forums)
                    forumComboBox.Items.Add(fr);
                
            }
            else if (currentID == dataCollection.Count)
            {
                idText.Text = string.Empty;
                urlText.Text = string.Empty;
                nameText.Text = string.Empty;
                AllGrid.Visibility = Visibility.Collapsed;
                loginTextBlock.Text = string.Empty;
                typeText.Text = string.Empty;
                domainTextBox.Text = string.Empty;
            }
        }

        private void PreviouseButtonClick(object sender, RoutedEventArgs e)
        {
            if (currentID > -1) currentID--;
            forumComboBox.Items.Clear();
            if (currentID >= 0)
            {
                idText.Text = dataCollection[currentID].ID.ToString();
                urlText.Text = dataCollection[currentID].Url;
                nameText.Text = dataCollection[currentID].Name;
                AllGrid.Visibility = Visibility.Visible;

                loginTextBlock.Text = dataCollection[currentID].UserObject.Login;
                //passwordTextBlock.Text = dataCollection[currentID].UserObject.Password;
                domainTextBox.Text = dataCollection[currentID].Url;
                typeText.Text = dataCollection[currentID].SiteType;
                foreach (Forum fr in dataCollection[currentID].Forums)
                    forumComboBox.Items.Add(fr);
            }
            else if (currentID == -1)
            {
                idText.Text = string.Empty;
                urlText.Text = string.Empty;
                nameText.Text = string.Empty;
                AllGrid.Visibility = Visibility.Collapsed;
                loginTextBlock.Text = string.Empty;
                typeText.Text = string.Empty;
//                passwordTextBlock.Text = string.Empty;
                domainTextBox.Text = string.Empty;
            }
        }

        private void OpenDBUttonClick(object sender, RoutedEventArgs e)
        {
            FillGrid();
            if (dataCollection[currentID] != null)
                foreach (Forum fr in dataCollection[currentID].Forums)
                    forumComboBox.Items.Add(fr);
        }
    }
}
